package com.gzsxy.sso.core.controller;


import com.gzsxy.sso.common.dto.request.RoleRequest;
import com.gzsxy.sso.common.result.R;
import com.gzsxy.sso.core.entity.Role;
import com.gzsxy.sso.core.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  用户角色管理
 * </p>
 *
 * @author zxl
 * @since 2021-11-10
 */
@Api(description="用户角色管理")
@CrossOrigin  //解决跨域
@RestController
@RequestMapping("/admin/userRole")
public class UserRoleController {

    @Autowired
    private UserRoleService userRoleService;



    @ApiOperation("根据用户id添加角色列表")
    @PutMapping("/users/{userId}/role")
    public R addRoleByuserId(@ApiParam(value = "用户id",required = true) @PathVariable String userId,
                             @RequestBody RoleRequest roleRequest){
        userRoleService.addRoleByuserId(userId,roleRequest.getRoleIds());
        return R.ok().message("添加成功");

    }


    @ApiOperation("通过用户id查询对应的所有角色")
    @GetMapping("/findRolesByUserId")
    public R findRolesByUserId(@RequestParam String userId){
        List<Role> roleList = userRoleService.findRolesByUserId(userId);
        return R.ok().message("查询成功").data("roles",roleList);
    }




}

